import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'
import { resolve } from 'path'
import dts from 'vite-plugin-dts'
import vueJsx from '@vitejs/plugin-vue-jsx'
import { externalDeps, globalVarMap } from './build.config'

// 检查是否是lib模式
const isLib = process.argv.includes('--mode') && process.argv.includes('lib')
// 检查是否是单一输出文件模式
const isSingleOutput = true//process.env.SINGLE_OUTPUT === 'true'
console.log(isSingleOutput,'isSingleOutput',isLib)
export default defineConfig({
  plugins: [
    vue(),
    vueJsx(),
    dts({
      insertTypesEntry: true,
      copyDtsFiles: false,
      outDir: 'dist',
      staticImport: true,
      // 在lib模式下跳过类型检查
      ...(isLib ? { skipTypeCheck: true } : {})
    })
  ],
  css: {
    preprocessorOptions: {
      scss: {
        additionalData: `@import "@/styles/variables.scss";`
      }
    }
  },
  build: {
    lib: {
      entry: resolve(__dirname, 'src/index.ts'),
      name: 'EpmsElement',
      fileName: (format) => `index.${format === 'es' ? 'mjs' : 'js'}`
    },
    rollupOptions: {
      external: externalDeps,
      output: {
        exports: 'named',
        globals: globalVarMap,
        // 根据模式选择是否启用preserveModules
        ...(isSingleOutput 
          ? { 
              inlineDynamicImports: true, 
              manualChunks: undefined 
            } 
          : { 
              preserveModules: true,
              preserveModulesRoot: 'src' 
            }
        )
      }
    },
    cssCodeSplit: false,
    sourcemap: true,
    minify: 'terser'
  },
  resolve: {
    alias: {
      '@': resolve(__dirname, './src')
    }
  },
  define: {
    __VUE_OPTIONS_API__: true,
    __VUE_PROD_DEVTOOLS__: false
  }
}) 